package com.mofang.powerdekorhelper.activity.shopactivity;

import android.app.Dialog;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.model.ShopActiveDetail;
import com.mofang.powerdekorhelper.persenter.ShopActiveDetailPerenter;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.NetUtiles;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.ShopActiveDetailView;
import com.mofang.powerdekorhelper.witget.MyWebViewClient;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/7/13 0013.
 */

public class ShopActiveDetailActivity extends MvpActivity<ShopActiveDetailView, ShopActiveDetailPerenter> implements ShopActiveDetailView {

    @BindView(R.id.shop_activity_detail_title)
    TitleBar mTitleBar;

    @BindView(R.id.activity_detail_title)
    TextView articleTitleTv;

    @BindView(R.id.shop_activity_detail_author)
    TextView articleAuthorTv;

    @BindView(R.id.shop_activity_detail_time)
    TextView articleTimeTv;

    /*@BindView(R.id.shop_activity_detail_imv)
    ImageView articleImv;*/

    @BindView(R.id.shop_activity_detail_web)
    WebView mWebView;

    @BindView(R.id.shop_activity_detail_start_time)
    TextView startTimeTv;

    @BindView(R.id.shop_activity_detail_end_time)
    TextView endTimeTv;

    @BindView(R.id.shop_activity_detail_commit_btn)
    TextView commitBtn;

    private int activityId;

    private int factoryId;

    private String qrImageUrl;

    private LoadPrograss loadPrograss;

    private Dialog sharedDialog = null;

    private File localQrcodeImage;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_shop_active_detail_laoyut);
        initTitleBar();
        loadPrograss = new LoadPrograss(this);
    }

    @Override
    public void initData() {

        showProgress();

        activityId = getIntent().getIntExtra("ActivityId", -1);

        presenter.getShopActiveDetail(activityId);

        factoryId = SharePerforenceUtils.getInstance(this).getFactoryId();
    }

    @Override
    public void setShopActiveDetail(ShopActiveDetail shopActiveDetail) {

        if (shopActiveDetail.getResult() != null) {

            articleTitleTv.setText(shopActiveDetail.getResult().getTitle());

            articleAuthorTv.setText("作者名称：");

            articleTimeTv.setText(shopActiveDetail.getResult().getCreate_time().split(" ")[0]);

            startTimeTv.setText(shopActiveDetail.getResult().getStart_time().split(" ")[0]);

            endTimeTv.setText(shopActiveDetail.getResult().getEnd_time().split(" ")[0]);

            //PicassoUtils.setImageUrl(this, shopActiveDetail.getResult().getImage(), articleImv);

            WebSettings webSettings = mWebView.getSettings();//获取webview设置属性
            webSettings.setJavaScriptEnabled(true);//支持js
            mWebView.setWebViewClient(new MyWebViewClient());
            mWebView.loadDataWithBaseURL(null, shopActiveDetail.getResult().getContent(), "text/html", "utf-8", null);
        }
    }

    @Override
    public void setQrCode(ResultMessage4 resultMessage) {

        if (resultMessage.getResult() != null && !resultMessage.getResult().equals("")) {
            qrImageUrl = resultMessage.getResult();

            if (sharedDialog == null) {

                sharedDialog = DialogUtils.MySharedDialog(this, new ShareDialogListener());
            }
            sharedDialog.show();
        }

        showProgress();

        new NetUtiles().bitmapCorvert(this, qrImageUrl, R.string.share_activity_title,"", articleTitleTv.getText().toString(), new NetUtiles.ICorvertImage() {
            @Override
            public void corvertSuccess(File file) {
                localQrcodeImage = file;
                hideProgress();
            }

            @Override
            public void covertFail() {

            }
        });

    }

    @OnClick({R.id.shop_activity_detail_commit_btn})
    void click(View view) {

        switch (view.getId()) {

            case R.id.shop_activity_detail_commit_btn:

                // 有小程序
                // 参数Id待定
                presenter.getQrCode("1", factoryId, 3);

                break;
        }
    }

    class ShareDialogListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.share_moments_imv:

                    /*new ShareUtils(ShopActiveDetailActivity.this)
                            .shareImage(ShopActiveDetailActivity.this, SHARE_MEDIA.WEIXIN_CIRCLE, qrImageUrl);*/

                    /*new ShareUtils(ShopActiveDetailActivity.this)
                            .shareLocalImage(ShopActiveDetailActivity.this, SHARE_MEDIA.WEIXIN_CIRCLE, localQrcodeImage, articleTitleTv.getText().toString());*/

                    break;

                case R.id.share_wechat_imv:

                    /*new ShareUtils(ShopActiveDetailActivity.this)
                            .shareImage(ShopActiveDetailActivity.this, SHARE_MEDIA.WEIXIN, qrImageUrl);*/

                   /* new ShareUtils(ShopActiveDetailActivity.this)
                            .shareLocalImage(ShopActiveDetailActivity.this, SHARE_MEDIA.WEIXIN, localQrcodeImage, articleTitleTv.getText().toString());*/
                    break;

                case R.id.share_sina_imv:

                   /* new ShareUtils(ShopActiveDetailActivity.this)
                            .shareLocalImage(ShopActiveDetailActivity.this, SHARE_MEDIA.SINA, localQrcodeImage, articleTitleTv.getText().toString());*/

                    break;

                case R.id.share_cancle_tv:
                    sharedDialog.dismiss();
                    break;
            }
            sharedDialog.dismiss();
        }
    }

    private void initTitleBar() {
        initTitleBarWithback(mTitleBar, R.string.activity_detial_title);
        mTitleBar.setDividerColor(getResources().getColor(R.color.grey_deep));
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {
        T.showShort(this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {
        hideProgress();
    }

    @Override
    public void hideNullLayout() {
        hideProgress();
    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {
        hideProgress();
    }

    @Override
    public void onError(String errorInfo) {

    }

    @Override
    public ShopActiveDetailPerenter initPresenter() {
        return new ShopActiveDetailPerenter();
    }

}

